package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.service.handler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.AliyunVodSDKUtils;
import com.atguigu.vod.utils.ConstantVideoObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Classname VodServiceImpl
 * @Description TODO
 * @Date 2022/9/3 14:19
 * @Created by ZHL17
 */
@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideo(MultipartFile file) {
        String videoId=null;
        try {
            InputStream inputStream=file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String title=originalFilename.substring(0,originalFilename.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(ConstantVideoObject.KEY_ID, ConstantVideoObject.KEY_SECRET, title,originalFilename,inputStream);
            UploadVideoImpl uploadVideo=new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            videoId=response.getVideoId();
            if(!response.isSuccess()) {
                String error = "阿里云上传错误：" + "code:" + response.getCode() + ",message:" + response.getMessage();
                throw new GuliException(20001, error);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoId;
    }

    @Override
    public void deleteVideoById(String id) {
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVideoObject.KEY_ID, ConstantVideoObject.KEY_SECRET);
        DeleteVideoRequest request=new DeleteVideoRequest();
        request.setVideoIds(id);
        try {
            client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new GuliException(20001,"视频删除失败！");
        }


    }

    @Override
    public void removeVideoList(List<String> videoIdList) {
        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVideoObject.KEY_ID, ConstantVideoObject.KEY_SECRET);
        //创建请求对象,一次只能批量删除20个
        String str= StringUtils.join(videoIdList.toArray(),",");
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(str);
        try {
            client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new GuliException(20001,"视频删除失败！");
        }
    }
}
